package com.restaurant.validators;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.restaurant.domain.Foodtype;
import com.restaurant.domain.Menu;
import com.restaurant.service.MenuService;

public class FoodTypeFormDeleteValidator implements Validator{

	@Autowired
	private MenuService objMenuService;
	
	@Override
	public boolean supports(Class<?> arg0) {
		return Foodtype.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		Foodtype foodtype = (Foodtype) arg0;
		ArrayList<Menu> listMenus = new ArrayList<Menu>();
		try {
			listMenus = (ArrayList<Menu>) objMenuService.loadByHql("from Menu where foodtype.foodTypeID = " + foodtype.getFoodTypeID());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(listMenus.size()>0){
			errors.rejectValue("alreadyExists", "foodtype.exists");
		}
		
	}

}
